﻿using SIIE_ERP.BaseDatos.Bancos.Catalogos;
using SIIE_ERP.BaseDatos.Inventarios.Catalogos;
using SIIE_ERP.Codigo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIIE_ERP.BaseDatos.Configuracion
{
    public class dbo_Trans_TiempoAire_RFC
    {
        #region Propiedades

        private string _RFC;
        private string _Usuario;

        #endregion
        
        #region Constructores

        public dbo_Trans_TiempoAire_RFC(string RFC, string Usuario)
        {
            this._RFC = RFC;
            this._Usuario = Usuario;
        }

        #endregion
        
        #region INSERT

        public Respuesta Insert_TiempoAire_RFC()
        {
            double Comision_Telcel = 5;
            double Comision_Movistar = 3.5;
            double Comision_Unefon = 5.5;
            double Comision_Nextel = 5.5;
            double Comision_Iusacell = 5.5;

            string Query = @"
                
                INSERT INTO dbo_Trans_TiempoAire_RFC 
                (
                 	Id_TiempoAire_RFC,
	                RFC,
	                Saldo_Telcel,
	                Saldo_Otros,
	                Comision_Telcel,
	                Comision_Movistar,
	                Comision_Unefon,
	                Comision_Nextel,
	                Comision_Iusacell,
	                Telcel_MerchantId,
	                Telcel_SiteId,
	                Telcel_ClerkId,
	                NoTelcel_MerchantId,
	                NoTelcel_SiteId,
	                NoTelcel_ClerkId,
	                Usuario_Alta,
	                Fecha_Alta,
	                Usuario_Mantto,
	                Fecha_Mantto
                )
                VALUES
                (
                    IIF(DCount('*', 'dbo_Trans_TiempoAire_RFC') = 0, 1, DMax('Id_TiempoAire_RFC', 'dbo_Trans_TiempoAire_RFC') + 1),
                    '@@RFC',
                    0,
                    0,
                    @@Comision_Telcel,
                    @@Comision_Movistar,
                    @@Comision_Unefon,
                    @@Comision_Nextel,
                    @@Comision_Iusacell,
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '@@Usuario_Alta',
                    NOW(),
                    '@@Usuario_Mantto',
                    NOW()
                )
            ".Replace("@@RFC", this._RFC)
             .Replace("@@Comision_Telcel", Comision_Telcel.ToString())
             .Replace("@@Comision_Movistar", Comision_Movistar.ToString())
             .Replace("@@Comision_Unefon", Comision_Unefon.ToString())
             .Replace("@@Comision_Nextel", Comision_Nextel.ToString())
             .Replace("@@Comision_Iusacell", Comision_Iusacell.ToString())
             .Replace("@@Usuario_Alta", this._Usuario)
             .Replace("@@Usuario_Mantto", this._Usuario);

            return SrvDB.ExecuteQuery_Insert(Query);
        }

        public Respuesta Insert_ArticulosTA()
        {
            List<ArticulosTA> ListaTa = new List<ArticulosTA>();

            ListaTa.Add(new ArticulosTA(0, "5020120", "Recarga IUSACELL 200", 200, 4));
            ListaTa.Add(new ArticulosTA(0, "5020150", "Recarga IUSACELL 500", 500, 4));
            ListaTa.Add(new ArticulosTA(0, "5021150", "Recarga Telcel 150", 150, 1));
            ListaTa.Add(new ArticulosTA(0, "5021100", "Recarga Telcel 100", 100, 1));
            ListaTa.Add(new ArticulosTA(0, "5021300", "Recarga Telcel 300", 300, 1));
            ListaTa.Add(new ArticulosTA(0, "5016100", "Recarga UNEFON 100", 100, 3));
            ListaTa.Add(new ArticulosTA(0, "5016200", "Recarga UNEFON 200", 200, 3));
            ListaTa.Add(new ArticulosTA(0, "5020130", "Recarga IUSACELL 300", 300, 4));
            ListaTa.Add(new ArticulosTA(0, "5037030", "Recarga Nextel 30", 30, 5));
            ListaTa.Add(new ArticulosTA(0, "5037500", "Recarga Nextel 500", 500, 5));
            ListaTa.Add(new ArticulosTA(0, "5016300", "Recarga UNEFON 300", 300, 3));
            ListaTa.Add(new ArticulosTA(0, "5021050", "Recarga Telcel 50", 50, 1));
            ListaTa.Add(new ArticulosTA(0, "5016500", "Recarga UNEFON 500", 500, 3));
            ListaTa.Add(new ArticulosTA(0, "5016050", "Recarga UNEFON 50", 50, 3));
            ListaTa.Add(new ArticulosTA(0, "5020115", "Recarga IUSACELL 150", 150, 4));
            ListaTa.Add(new ArticulosTA(0, "5017000", "Recarga UNEFON 1000", 1000, 3));
            ListaTa.Add(new ArticulosTA(0, "5000060", "Recarga Movistar 60", 60, 2));
            ListaTa.Add(new ArticulosTA(0, "5000500", "Recarga Movistar 500", 500, 2));
            ListaTa.Add(new ArticulosTA(0, "5037100", "Recarga Nextel 100", 100, 5));
            ListaTa.Add(new ArticulosTA(0, "5037050", "Recarga Nextel 50", 50, 5));
            ListaTa.Add(new ArticulosTA(0, "5000120", "Recarga Movistar 20", 20, 2));
            ListaTa.Add(new ArticulosTA(0, "5000300", "Recarga Movistar 300", 300, 2));
            ListaTa.Add(new ArticulosTA(0, "5000030", "Recarga Movistar 30", 30, 2));
            ListaTa.Add(new ArticulosTA(0, "5020200", "Recarga IUSACELL 1000", 1000, 4));
            ListaTa.Add(new ArticulosTA(0, "5037200", "Recarga Nextel 200", 200, 5));
            ListaTa.Add(new ArticulosTA(0, "5000100", "Recarga Movistar 100", 100, 2));
            ListaTa.Add(new ArticulosTA(0, "5021500", "Recarga Telcel 500", 500, 1));
            ListaTa.Add(new ArticulosTA(0, "5021030", "Recarga Telcel 30", 30, 1));
            ListaTa.Add(new ArticulosTA(0, "5021020", "Recarga Telcel 20", 20, 1));
            ListaTa.Add(new ArticulosTA(0, "5021200", "Recarga Telcel 200", 200, 1));
            ListaTa.Add(new ArticulosTA(0, "5000200", "Recarga Movistar 200", 200, 2));
            ListaTa.Add(new ArticulosTA(0, "5020105", "Recarga IUSACELL 50", 50, 4));
            ListaTa.Add(new ArticulosTA(0, "5016150", "Recarga UNEFON 150", 150, 3));
            ListaTa.Add(new ArticulosTA(0, "5020110", "Recarga IUSACELL 100", 100, 4));
            ListaTa.Add(new ArticulosTA(0, "5000110", "Recarga Movistar 10", 10, 2));
            ListaTa.Add(new ArticulosTA(0, "5000112", "Recarga Movistar 120", 120, 2));

            foreach (ArticulosTA a in ListaTa)
            {
                dbo_Cat_Articulo cat_a = new dbo_Cat_Articulo(_RFC, _Usuario);
                dbo_Cat_Moneda cat_m = new dbo_Cat_Moneda(_RFC, _Usuario);
                
                int Id_Moneda = Int32.Parse(cat_m.Get_Moneda_Por_Nombre("Pesos Mexicanos").Rows[0]["Id_Moneda"].ToString());

                cat_a.Insert_Articulo(a.CompaniaCelular.ToString(), a.Descripcion, "RECARGA CEL", a.ProductCode, "", "", 4, -1, -1, -1, -1, -1, -1, false, false, Id_Moneda, a.Monto, a.Monto, -1, 1, 0, 0, 0, 0,0,0);
            }

            return new Respuesta(1, "Exito");
        }

        public class ArticulosTA
        {
            private string _ProductCode;
            private string _Descripcion;
            private int _CompaniaCelular;
            private int _Monto;
            private int _Id_Articulo;
                
            public int Monto
            {
                get { return _Monto; }
                set { _Monto = value; }
            }

            public int Id_Articulo
            {
                get { return _Id_Articulo; }
                set { _Id_Articulo = value; }
            }

            public int CompaniaCelular
            {
                get { return _CompaniaCelular; }
                set { _CompaniaCelular = value; }
            }

            public string ProductCode
            {
                get { return _ProductCode; }
                set { _ProductCode = value; }
            }

            public string Descripcion
            {
                get { return _Descripcion; }
                set { _Descripcion = value; }
            }

            public ArticulosTA(int Id_Articulo, string ProductCode, string Descripcion, int Monto, int CompaniaCelular)
            {
                this._Id_Articulo = Id_Articulo;
                this._CompaniaCelular = CompaniaCelular;
                this._Descripcion = Descripcion;
                this._ProductCode = ProductCode;
                this._Monto = Monto;
            }
        }

        #endregion

        #region UPDATE

        public Respuesta UpdateClaves
        (
            string Telcel_MerchantId,
            string Telcel_SiteId,
            string Telcel_ClerkId,
            string NoTelcel_MerchantId,
            string NoTelcel_SiteId,
            string NoTelcel_ClerkId
        )
        {
            string Query = @"

                UPDATE dbo_Trans_TiempoAire_RFC
                    SET 
                         Telcel_MerchantId = @@Telcel_MerchantId,
                         Telcel_SiteId = @@Telcel_SiteId,
                         Telcel_ClerkId = @@Telcel_ClerkId,
                         NoTelcel_MerchantId = @@NoTelcel_MerchantId,
                         NoTelcel_SiteId = @@NoTelcel_SiteId,
                         NoTelcel_ClerkId = @@NoTelcel_ClerkId
                    WHERE RFC = '@@RFC'
            ".Replace("@@Telcel_MerchantId", Telcel_MerchantId.ToString())
             .Replace("@@Telcel_SiteId", Telcel_SiteId.ToString())
             .Replace("@@Telcel_ClerkId", Telcel_ClerkId.ToString())
             .Replace("@@NoTelcel_MerchantId", NoTelcel_MerchantId.ToString())
             .Replace("@@NoTelcel_SiteId", NoTelcel_SiteId.ToString())
             .Replace("@@NoTelcel_ClerkId", NoTelcel_ClerkId.ToString())
             .Replace("@@RFC", this._RFC);

            return SrvDB.ExecuteQuery_Update(Query);
        }
        
        #endregion

        #region DELETE

        
        #endregion

        #region SELECT

        public static bool IsTiempoAireActivo(string RFC)
        {
            string Query = @"
            
                SELECT * FROM dbo_Trans_TiempoAire_RFC WHERE RFC = '@@RFC'
            ".Replace("@@RFC", RFC);

            if (SrvDB.ExecuteQuery_Get(Query).Rows.Count > 0)
                return true;
            return false;
        }

        public static bool IsTiempoAireInProcess(string RFC)
        {
            if (IsTiempoAireActivo(RFC)) 
            {
                string Query = @"
            
                    SELECT * FROM dbo_Trans_TiempoAire_RFC WHERE RFC = '@@RFC' 
                ".Replace("@@RFC", RFC);

                DataTable dt = SrvDB.ExecuteQuery_Get(Query);
                
                if (String.IsNullOrEmpty(dt.Rows[0]["Telcel_MerchantId"].ToString()) &&
                    String.IsNullOrEmpty(dt.Rows[0]["Telcel_SiteId"].ToString()) &&
                    String.IsNullOrEmpty(dt.Rows[0]["Telcel_ClerkId"].ToString()) &&
                    String.IsNullOrEmpty(dt.Rows[0]["NoTelcel_MerchantId"].ToString()) &&
                    String.IsNullOrEmpty(dt.Rows[0]["NoTelcel_SiteId"].ToString()) &&
                    String.IsNullOrEmpty(dt.Rows[0]["NoTelcel_ClerkId"].ToString()) 
                    )
                    return true;
                return false;            
            }
            return false;
        }

        public DataTable Get_TiempoAire_RFC()
        {
            string Query = @"
                
                SELECT * FROM dbo_Trans_TiempoAire_RFC WHERE RFC = '@@RFC'
            ".Replace("@@RFC", this._RFC);

            return SrvDB.ExecuteQuery_Get(Query);
        }

        #endregion
    }
}
